// CSelfCheckDlg.cpp : implementation file
//

#include "stdafx.h"
#include <streams.h>
#include "AVChat.h"
#include "CSelfCheckDlg.h"

#include "CAVDevice.h"
#include "CAudioCaptureFilter2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSelfCheckDlg dialog

CSelfCheckDlg::CSelfCheckDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSelfCheckDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSelfCheckDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	mVideoDevice = NULL;
	mAudioDevice = NULL;
	mDeviceConfig = 0;
}


void CSelfCheckDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSelfCheckDlg)
	DDX_Control(pDX, IDC_COMBO_AUDIO_INPUTS, mComboAudioInputs);
	DDX_Control(pDX, IDC_ADJUST_WINDOW, mVideoWindow);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSelfCheckDlg, CDialog)
	//{{AFX_MSG_MAP(CSelfCheckDlg)
	ON_BN_CLICKED(IDC_BUTTON_AUDIO_CAPTURE, OnButtonAudioCapture)
	ON_BN_CLICKED(IDC_BUTTON_VIDEO_CAPTURE, OnButtonVideoCapture)
	ON_CBN_SELCHANGE(IDC_COMBO_AUDIO_INPUTS, OnSelchangeComboAudioInputs)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSelfCheckDlg message handlers
void CSelfCheckDlg::SetAVDevice(CAVDevice * inVideo, CAVDevice * inAudio)
{
	mVideoDevice = inVideo;
	mAudioDevice = inAudio;
	mDeviceAdmin.SetAVDevice(inVideo, inAudio);
}

BOOL CSelfCheckDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	mDeviceAdmin.SetVideoWindow(&mVideoWindow);
	if (mDeviceAdmin.Activate())
	{
		FillAudioConnectors();
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSelfCheckDlg::FillAudioConnectors(void)
{
	mComboAudioInputs.ResetContent();

	CAudioCaptureFilter2 * pFilter = NULL;
	pFilter = (CAudioCaptureFilter2 *) mDeviceAdmin.GetAudioInputFilter();
	if (pFilter)
	{
		long count = pFilter->GetConnectorCount();
		for (long i = 0; i < count; i++)
		{
			mComboAudioInputs.AddString(pFilter->GetConnectorName(i));
		}

		mComboAudioInputs.SetCurSel(pFilter->GetConnector());
	}
}

void CSelfCheckDlg::OnSelchangeComboAudioInputs() 
{
	int index = mComboAudioInputs.GetCurSel();
	if (index >= 0)
	{
		CAudioCaptureFilter2 * pFilter = NULL;
		pFilter = (CAudioCaptureFilter2 *) mDeviceAdmin.GetAudioInputFilter();
		if (pFilter)
		{
			pFilter->SetConnector(index);
			mAudioDevice->SetAudioConnector(index);
		}
	}
}

void CSelfCheckDlg::OnButtonAudioCapture() 
{
	mDeviceAdmin.ShowAudioCapturePropertyPage();
}

void CSelfCheckDlg::OnButtonVideoCapture() 
{
	mDeviceAdmin.ShowVideoCapturePropertyPage();
}

long CSelfCheckDlg::GetDeviceConfig(void)
{
	return mDeviceConfig;
}

// Gather device info: has audio? has video?
void CSelfCheckDlg::OnDestroy() 
{
	mDeviceAdmin.Deactivate();

	// Video device
	int isVideoDisable = IsDlgButtonChecked(IDC_CHECK_VIDEO_DISABLE);
	if (mVideoDevice->IsDeviceAvailable() && !isVideoDisable)
	{
		mDeviceConfig |= Local_Has_Video;
	}

	// Audio device
	int isAudioDisable = IsDlgButtonChecked(IDC_CHECK_AUDIO_DISABLE);
	if (mAudioDevice->IsDeviceAvailable() && !isAudioDisable)
	{
		mDeviceConfig |= Local_Has_Audio;
	}

	CDialog::OnDestroy();	
}
